/**********************************************************************/
/*
   Title: match_unmatched_agents.do
   Author: Robbie Dulin, Clotaire Boyer
   Created: 19 June 2020
   Description: This file matches unmatched agents to villages in PODES
    2019.
*/
/**********************************************************************/

// NOTE: Bank requested full confidentiality. Therefore, this portion 
// of the code is heavily redacted, and no log file is provided publically.


/*----------------------------------------------------*/
                  /* Section 0: Setup */
/*----------------------------------------------------*/

clear
set more off


* open log file
  cap log close
  local prefix: display %tdCYND td(`c(current_date)')
  log using "$log/`prefix'_match_unmatched_agents", replace text


/*----------------------------------------------------*/
     /* Section 1: Match REDACTED and REDACTED agents */
/*----------------------------------------------------*/


* load BANK2
  u "$agents_mar_18/baseline_BANK2_agent_march2018_notmatch.dta", clear
  isid TID
  count if NamaProvinsi != "" & NamaKabupatenKota != "" & NamaKecamatan != "" & NamaDesaKel != ""
  keep if NamaProvinsi != "" & NamaKabupatenKota != "" & NamaKecamatan != "" & NamaDesaKel != ""
  gen bank = "BANK2"

  rename TID KODEAGEN
  keep KODEAGEN Nama* bank
  di _N

  tempfile BANK2
  qui save `BANK2'

* load BANK3
  u "$agents_mar_18/baseline_BANK3_agent_march2018_notmatch.dta", clear
  isid KODEAGEN

  keep if BANK3 == 1
  count if NamaProvinsi != "" & NamaKabupatenKota != "" & NamaKecamatan != "" & NamaDesaKel != ""
  keep if NamaProvinsi != "" & NamaKabupatenKota != "" & NamaKecamatan != "" & NamaDesaKel != ""
  gen bank = "BANK3"

  keep KODEAGEN Nama* bank
  di _N

* append
  append using `BANK2'
  di _N

** keep only agents in A/B districts

  * rename kabupatens to match names in PODES data
  replace NamaKabupatenKota = "KOTA LANGSA"           if NamaKabupatenKota == "LANGSA"              & NamaProvinsi == "ACEH"
  replace NamaKabupatenKota = "MUKOMUKO"              if NamaKabupatenKota == "MUKO MUKO"           & NamaProvinsi == "BENGKULU"
  replace NamaKabupatenKota = "KOTA B A T A M"        if NamaKabupatenKota == "KOTA BATAM"          & NamaProvinsi == "KEPULAUAN RIAU"
  replace NamaKabupatenKota = "TULANG BAWANG BARAT"   if NamaKabupatenKota == "TULANGBAWANG BARAT"  & NamaProvinsi == "LAMPUNG"
  replace NamaKabupatenKota = "LUWU TIMUR"            if NamaKabupatenKota == "LUWUTIMUR"           & NamaProvinsi == "SULAWESI SELATAN"
  replace NamaKabupatenKota = "KOTA SAWAH LUNTO"      if NamaKabupatenKota == "KOTA SAWAHLUNTO"     & NamaProvinsi == "SUMATERA BARAT"
  replace NamaKabupatenKota = "PASAMAN BARAT"         if NamaKabupatenKota == "PASAMANBARAT"        & NamaProvinsi == "SUMATERA BARAT"
  replace NamaKabupatenKota = "PESISIR SELATAN"       if NamaKabupatenKota == "PESISIRSELATAN"      & NamaProvinsi == "SUMATERA BARAT"
  replace NamaKabupatenKota = "KOTA LUBUKLINGGAU"     if NamaKabupatenKota == "KOTA LUBUK LINGGAU"  & NamaProvinsi == "SUMATERA SELATAN"
  replace NamaKabupatenKota = "LABUHAN BATU"          if NamaKabupatenKota == "LABUHANBATU"         & NamaProvinsi == "SUMATERA UTARA"
  replace NamaKabupatenKota = "LABUHAN BATU UTARA"    if NamaKabupatenKota == "LABUHANBATUUTAR"     & NamaProvinsi == "SUMATERA UTARA"

  * rename kabupatens to match names in A/B assignment data
  replace NamaKabupatenKota = "GUNUNGKIDUL"           if NamaKabupatenKota == "GUNUNG KIDUL"        & NamaProvinsi == "DI YOGYAKARTA"
  replace NamaKabupatenKota = "KOTA BATAM"            if NamaKabupatenKota == "KOTA B A T A M"      & NamaProvinsi == "KEPULAUAN RIAU"
  replace NamaKabupatenKota = "KOTA TANJUNGPINANG"    if NamaKabupatenKota == "KOTA TANJUNG PINANG" & NamaProvinsi == "KEPULAUAN RIAU"
  replace NamaKabupatenKota = "KOTA BANDARLAMPUNG"    if NamaKabupatenKota == "KOTA BANDAR LAMPUNG" & NamaProvinsi == "LAMPUNG"
  replace NamaKabupatenKota = "KOTA SAWAHLUNTO"       if NamaKabupatenKota == "KOTA SAWAH LUNTO"    & NamaProvinsi == "SUMATERA BARAT"
  replace NamaKabupatenKota = "BATUBARA"              if NamaKabupatenKota == "BATU BARA"           & NamaProvinsi == "SUMATERA UTARA"
  replace NamaKabupatenKota = "KARANGASEM"            if NamaKabupatenKota == "KARANG ASEM"         & NamaProvinsi == "BALI"
  replace NamaKabupatenKota = "KOTA BANJARBARU"       if NamaKabupatenKota == "KOTA BANJAR BARU"    & NamaProvinsi == "KALIMANTAN SELATAN"
  replace NamaKabupatenKota = "KOTA KOTAMOBAGU"       if NamaKabupatenKota == "KOTAMOBAGU"          & NamaProvinsi == "SULAWESI UTARA"

  * possible matches: (kec and desa located in different kabupaten than reported)
  replace NamaKabupatenKota = "TANGERANG"               if NamaKabupatenKota == "KOTA TANGERANG"     & NamaKecamatan == "PASAR KEMIS" & NamaDesaKel == "KUTA JAYA"
  replace NamaKabupatenKota = "KOTA TANGERANG SELATAN"  if NamaKabupatenKota == "KOTA TANGERANG"     & NamaKecamatan == "CIPUTAT"     & NamaDesaKel == "SARUA"
  replace NamaKabupatenKota = "SLEMAN"                  if NamaKabupatenKota == "KOTA YOGYAKARTA"    & NamaKecamatan == "SYEGAN"      & NamaDesaKel == "MARGOKATON"
  replace NamaKabupatenKota = "KOTA JAKARTA TIMUR"      if NamaKabupatenKota == "KOTA JAKARTA UTARA" & NamaKecamatan == "PULOGADUNG"  & NamaDesaKel == "PISANGAN TIMUR"
  replace NamaKabupatenKota = "KOTA JAKARTA TIMUR"      if NamaKabupatenKota == "KOTA JAKARTA UTARA" & NamaKecamatan == "JATINEGARA"  & NamaDesaKel == "CIPINANG BESAR"
  replace NamaKabupatenKota = "KOTA JAKARTA UTARA"      if NamaKecamatan == "TANJUNG PRIOK"          & NamaDesaKel == "PAPANGGO"
  replace NamaKabupatenKota = "KOTA JAKARTA UTARA"      if NamaKecamatan == "CILINCING"              & NamaDesaKel == "KALIBARU"
  replace NamaKabupatenKota = "DELI SERDANG"            if NamaKecamatan == "TANJUNG MORAWA"         & NamaDesaKel == "DALU XA"
  replace NamaKabupatenKota = "BOGOR"                   if NamaKecamatan == "BOJONGGEDE"             & NamaDesaKel == "SUSUKAN"

  replace NamaKabupatenKota = "BOGOR"                   if NamaKecamatan == "PARUNG"                 & NamaDesaKel == "COGREG"
  replace NamaProvinsi = "JAWA BARAT"                   if NamaKecamatan == "PARUNG"                 & NamaDesaKel == "COGREG"

  // questionable:
  replace NamaKabupatenKota = "KOTA JAKARTA UTARA"      if NamaKecamatan == "SUNTER"                 & NamaDesaKel == "SUNTER JAYA"
  replace NamaKecamatan = "TANJUNG PRIOK"               if NamaKecamatan == "SUNTER"                 & NamaKabupatenKota == "KOTA JAKARTA UTARA"


  // load assignment data
  preserve
  u "$agent_data/Supplementary data/20200214_groupAB_district_corrected.dta", clear
  rename provinsi NamaProvinsi
  rename kotakabupaten NamaKabupatenKota
  sort NamaProvinsi NamaKabupatenKota
  tempfile kabs_ab
  save `kabs_ab'
  restore

  // merge at district level
  merge m:1 NamaProvinsi NamaKabupatenKota using `kabs_ab'

  /*
  REDACTED
  */

  // keep only agents in A/B districts
  keep if _m == 3
  drop _m


  ** Attempt matching at kecamatan level using A/B agents
  destring idkab, replace

  * fix spelling issues in kecamatans
  replace NamaKecamatan = "DOLAT RAYAT"            if NamaKecamatan == "DOLAT RAKYAT"            & NamaKabupatenKota == "KARO"
  replace NamaKecamatan = "MUNTE"                  if NamaKecamatan == "MUNTHE"                  & NamaKabupatenKota == "KARO"
  replace NamaKecamatan = "MEDAN SUNGGAL"          if NamaKecamatan == "SUNGGAL"                 & NamaKabupatenKota == "KOTA MEDAN"
  replace NamaKecamatan = "BATU RAJA TIMUR"        if NamaKecamatan == "BATURJA TIMUR"           & NamaKabupatenKota == "OGAN KOMERING ULU"
  replace NamaKecamatan = "ILIR TIMUR II"          if NamaKecamatan == "ILR TIMUR II"            & NamaKabupatenKota == "KOTA PALEMBANG"
  replace NamaKecamatan = "LUBUK LINGGAU BARAT II" if NamaKecamatan == "LUBUKLINGGAU BARAT DUA"  & NamaKabupatenKota == "KOTA LUBUKLINGGAU"
  replace NamaKecamatan = "LUBUK LINGGAU BARAT I"  if NamaKecamatan == "LUBUKLINGGAU BARAT SATU" & NamaKabupatenKota == "KOTA LUBUKLINGGAU"
  replace NamaKecamatan = "LUBUK LINGGAU TIMUR I"  if NamaKecamatan == "LUBUKLINGGAU TIMUR SATU" & NamaKabupatenKota == "KOTA LUBUKLINGGAU"
  replace NamaKecamatan = "TANJUNG SENANG"         if NamaKecamatan == "TANJUNG SENENG"          & NamaKabupatenKota == "KOTA BANDARLAMPUNG"
  replace NamaKecamatan = "PANCORAN"               if NamaKecamatan == "PANCORAN MAS"            & NamaKabupatenKota == "KOTA JAKARTA SELATAN"
  replace NamaKecamatan = "JATINEGARA"             if NamaKecamatan == "JATINEGARA KAUM"         & NamaKabupatenKota == "KOTA JAKARTA TIMUR"
  replace NamaKecamatan = "KEMBANGAN"              if NamaKecamatan == "KEMBANGAN UTARA"         & NamaKabupatenKota == "KOTA JAKARTA BARAT"
  replace NamaKecamatan = "TAMAN SARI"             if NamaKecamatan == "TAMANSARI"               & NamaKabupatenKota == "KOTA JAKARTA BARAT"
  replace NamaKecamatan = "CILINCING"              if NamaKecamatan == "CLINCING"                & NamaKabupatenKota == "KOTA JAKARTA UTARA"
  replace NamaKecamatan = "BEJI"                   if NamaKecamatan == "BEIJI"                   & NamaKabupatenKota == "KOTA DEPOK"
  replace NamaKecamatan = "SEYEGAN"                if NamaKecamatan == "SYEGAN"                  & NamaKabupatenKota == "SLEMAN"
  replace NamaKecamatan = "CIBODAS"                if NamaKecamatan == "CIBODAS BARU"            & NamaKabupatenKota == "KOTA TANGERANG"
  replace NamaKecamatan = "LARANGAN"               if NamaKecamatan == "LARANGAN INDAH"          & NamaKabupatenKota == "KOTA TANGERANG"
  replace NamaKecamatan = "MANGARA BOMBANG"        if NamaKecamatan == "MANGARABOMBANG"          & NamaKabupatenKota == "TAKALAR"
  replace NamaKecamatan = "PATTALLASSANG"          if NamaKecamatan == "PATALLASSANG"            & NamaKabupatenKota == "GOWA"
  replace NamaKecamatan = "TANGAN-TANGAN"          if NamaKecamatan == "TANGAN TANGAN "          & NamaKabupatenKota == "ACEH BARAT DAYA"
  replace NamaKecamatan = "LIMA PULUH"             if NamaKecamatan == "LIMAPULUH"               & NamaKabupatenKota == "BATUBARA"
  replace NamaKecamatan = "KOTA PINANG"            if NamaKecamatan == "KOTAPINANG"              & NamaKabupatenKota == "LABUHAN BATU SELATAN"
  replace NamaKecamatan = "UJUNG JAYA"             if NamaKecamatan == "UJUNGJAYA"               & NamaKabupatenKota == "SUMEDANG"
  replace NamaKecamatan = "SUKMAJAYA"              if NamaKecamatan == "SUKMA JAYA"              & NamaKabupatenKota == "KOTA DEPOK"
  replace NamaKecamatan = "GEDANG SARI"            if NamaKecamatan == "GEDANGSARI"              & NamaKabupatenKota == "GUNUNGKIDUL"
  replace NamaKecamatan = "SAPTO SARI"             if NamaKecamatan == "SAPTOSARI"               & NamaKabupatenKota == "GUNUNGKIDUL"
  replace NamaKecamatan = "SUMBERMANJING"          if NamaKecamatan == "SUMBERMANJING WETAN"     & NamaKabupatenKota == "MALANG"
  replace NamaKecamatan = "CURAH DAMI"             if NamaKecamatan == "CURAHDAMI"               & NamaKabupatenKota == "BONDOWOSO"
  replace NamaKecamatan = "ASEMROWO"               if NamaKecamatan == "ASEM ROWO"               & NamaKabupatenKota == "KOTA SURABAYA"
  replace NamaKecamatan = "BANJAR BARU SELATAN"    if NamaKecamatan == "BANJARBARU SELATAN"      & NamaKabupatenKota == "KOTA BANJARBARU"
  replace NamaKecamatan = "KOTA BANGUN"            if NamaKecamatan == "KOTABANGUN"              & NamaKabupatenKota == "KUTAI KARTANEGARA"
  replace NamaKecamatan = "BANJAR BARU UTARA"      if NamaKecamatan == "BANJARBARU UTARA"        & NamaKabupatenKota == "KOTA BANJARBARU"
  replace NamaKecamatan = "PULO GADUNG"            if NamaKecamatan == "PULOGADUNG"              & NamaKabupatenKota == "KOTA JAKARTA TIMUR"
  replace NamaKecamatan = "BOJONG GEDE"            if NamaKecamatan == "BOJONGGEDE"              & NamaKabupatenKota == "BOGOR"
  replace NamaKecamatan = "PASARKEMIS"             if NamaKecamatan == "PASAR KEMIS"             & NamaKabupatenKota == "TANGERANG"

  * switching kecamatan and desa when they appear to be mixed up
  replace NamaKecamatan = "BONTOALA"               if NamaKecamatan == "LAYANG"                & NamaKabupatenKota == "KOTA MAKASSAR"
  replace NamaDesaKel = "LAYANG"                   if NamaDesaKel == "BONTOALA"                & NamaKecamatan == "BONTOALA"

  replace NamaKecamatan = "PANAKKUKANG"            if NamaKecamatan == "TELLO BARU"            & NamaKabupatenKota == "KOTA MAKASSAR"
  replace NamaDesaKel = "TELLO BARU"               if NamaDesaKel == "PANAKKUKANG"             & NamaKecamatan == "PANAKKUKANG"

  replace NamaKecamatan = "TAPOS"                  if NamaKecamatan == "CIMPAEUN"              & NamaKabupatenKota == "KOTA DEPOK"
  replace NamaDesaKel = "CIMPAEUN"                 if NamaDesaKel == "TAPOS"                   & NamaKecamatan == "TAPOS"

  replace NamaKecamatan = "CIMAHI TENGAH"          if NamaKecamatan == "BAROS"                 & NamaKabupatenKota == "KOTA CIMAHI"
  replace NamaDesaKel = "BAROS"                    if NamaDesaKel == "CIMAHI TENGAH"           & NamaKecamatan == "CIMAHI TENGAH"

  replace NamaKecamatan = "CIMAHI SELATAN"         if NamaKecamatan == "LEUWIGAJAH"            & NamaKabupatenKota == "KOTA CIMAHI"
  replace NamaDesaKel = "LEUWIGAJAH"               if NamaDesaKel == "CIMAHI SELATAN"          & NamaKecamatan == "CIMAHI SELATAN"

  replace NamaKecamatan = "CIPUTAT"                if NamaKecamatan == "JOMBANG"               & NamaKabupatenKota == "KOTA TANGERANG SELATAN"
  replace NamaDesaKel = "JOMBANG"                  if NamaDesaKel == "CIPUTAT"                 & NamaKecamatan == "CIPUTAT"

  replace NamaKecamatan = "SERPONG"                if NamaKecamatan == "LENGKONG GUDANG TIMUR" & NamaKabupatenKota == "KOTA TANGERANG SELATAN"
  replace NamaDesaKel = "LENGKONG GUDANG TIMUR"    if NamaDesaKel == "SERPONG"                 & NamaKecamatan == "SERPONG"

  replace NamaKecamatan = "CILINCING"              if NamaKecamatan == "SEMPER BARAT"          & NamaKabupatenKota == "KOTA JAKARTA UTARA"
  replace NamaDesaKel = "SEMPER BARAT"             if NamaDesaKel == "CILINCING"               & NamaKecamatan == "CILINCING"

  replace NamaKecamatan = "KEMBANGAN"              if NamaKecamatan == "SRENGSENG"             & NamaKabupatenKota == "KOTA JAKARTA BARAT"
  replace NamaDesaKel = "SRENGSENG"                if NamaDesaKel == "KEMBANGAN"               & NamaKecamatan == "KEMBANGAN"

  replace NamaKecamatan = "KEBON JERUK"            if NamaKecamatan == "DURI KEPA"             & NamaKabupatenKota == "KOTA JAKARTA BARAT"
  replace NamaDesaKel = "DURI KEPA"                if NamaDesaKel == "KEBON JERUK"             & NamaKecamatan == "KEBON JERUK"

  replace NamaKecamatan = "TAMBORA"                if NamaKecamatan == "DURI UTARA"            & NamaKabupatenKota == "KOTA JAKARTA BARAT"
  replace NamaDesaKel = "DURI UTARA"               if NamaDesaKel == "TAMBORA"                 & NamaKecamatan == "TAMBORA"

  replace NamaKecamatan = "PASAR MINGGU"           if NamaKecamatan == "KEBAGUSAN"             & NamaKabupatenKota == "KOTA JAKARTA SELATAN"
  replace NamaDesaKel = "KEBAGUSAN"                if NamaKecamatan == "PASAR MINGGU"          & NamaKecamatan == "PASAR MINGGU"

  replace NamaKecamatan = "KEBAYORAN LAMA"         if NamaKecamatan == "PONDOK PINANG"         & NamaKabupatenKota == "KOTA JAKARTA SELATAN"
  replace NamaDesaKel = "PONDOK PINANG"            if NamaDesaKel == "KEBAYORAN LAMA"          & NamaKecamatan == "KEBAYORAN LAMA"

  replace NamaKecamatan = "KRAMAT JATI"            if NamaKecamatan == "DUKUH"                 & NamaKabupatenKota == "KOTA JAKARTA TIMUR"
  replace NamaDesaKel = "DUKUH"                    if NamaDesaKel == "KRAMAT JATI"             & NamaKecamatan == "KRAMAT JATI"

  replace NamaKecamatan = "DUREN SAWIT"            if NamaKecamatan == "MALAKA SARI"           & NamaKabupatenKota == "KOTA JAKARTA TIMUR"
  replace NamaDesaKel = "MALAKA SARI"              if NamaDesaKel == "DUREN SAWIT"             & NamaKecamatan == "DUREN SAWIT"

  replace NamaKecamatan = "SAWAH BESAR"            if NamaKecamatan == "MANGGA DUA SELATAN"    & NamaKabupatenKota == "KOTA JAKARTA PUSAT"
  replace NamaDesaKel = "MANGGA DUA SELATAN"       if NamaDesaKel == "SAWAH BESAR"             & NamaKecamatan == "SAWAH BESAR"

  replace NamaKecamatan = "JOHAR BARU"             if NamaKecamatan == "TANAH TINGGI"          & NamaKabupatenKota == "KOTA JAKARTA PUSAT"
  replace NamaDesaKel = "TANAH TINGGI"             if NamaDesaKel == "JOHAR BARU"              & NamaKecamatan == "JOHAR BARU"

  replace NamaKecamatan = "SILUNGKANG"             if NamaKecamatan == "MUARO KALABAN"         & NamaKabupatenKota == "KOTA SAWAHLUNTO"
  replace NamaDesaKel = "MUARO KALABAN"            if NamaDesaKel == "SILUNGKANG"              & NamaKecamatan == "SILUNGKANG"

  replace NamaKecamatan = "TELANAIPURA"            if NamaKecamatan == "TELUK KENALI"          & NamaKabupatenKota == "KOTA JAMBI"
  replace NamaDesaKel = "TELUK KENALI"             if NamaDesaKel == "TELANAIPURA"             & NamaKecamatan == "TELANAIPURA"

  replace NamaKecamatan = "KEBAYORAN BARU"         if NamaKecamatan == "GUNUNG"                & NamaKabupatenKota == "KOTA JAKARTA SELATAN"
  replace NamaDesaKel = "GUNUNG"                   if NamaDesaKel == "KEBAYORAN BARU"          & NamaKecamatan == "KEBAYORAN BARU"

  replace NamaKecamatan = "KEBAYORAN BARU"         if NamaKecamatan == "KRAMAT PELA"           & NamaKabupatenKota == "KOTA JAKARTA SELATAN"
  replace NamaDesaKel = "KRAMAT PELA"              if NamaDesaKel == "KEBAYORAN BARU"          & NamaKecamatan == "KEBAYORAN BARU"

  replace NamaKecamatan = "CILANDAK"               if NamaKecamatan == "LEBAK BULUS"           & NamaKabupatenKota == "KOTA JAKARTA SELATAN"
  replace NamaDesaKel = "LEBAK BULUS"              if NamaDesaKel == "CILANDAK"                & NamaKecamatan == "CILANDAK"

  * probable matches
  replace NamaKecamatan = "TAPOS"                  if NamaKecamatan == "SUKAMAJU"              & NamaKabupatenKota == "KOTA DEPOK"
  replace NamaDesaKel = "SUKAMAJU BARU"            if NamaDesaKel == "TAPOS"                   & NamaKecamatan == "TAPOS"

  replace NamaKecamatan = "DUKUH PAKIS"            if NamaKecamatan == "DUKUH KUPANG"          & NamaKabupatenKota == "KOTA SURABAYA"
  replace NamaDesaKel = "GUNUNGSARI"               if NamaDesaKel == "GUNUNG SARI"             & NamaKecamatan == "DUKUH PAKIS"

  replace NamaKecamatan = "PEKALONGAN TIMUR"       if NamaKecamatan == "BAROS"                 & NamaKabupatenKota == "KOTA PEKALONGAN"
  replace NamaDesaKel = "KALI BAROS"               if NamaDesaKel == "PEKALONGAN TIMUR"        & NamaKecamatan == "PEKALONGAN TIMUR"

  replace NamaKecamatan = "MEDAN KOTA"             if NamaKecamatan == "BATANG KUIS"           & NamaDesaKel == "SUDIREJO I"
  replace NamaDesaKel = "SUDI REJO I"              if NamaDesaKel == "SUDIREJO I"              & NamaKecamatan == "MEDAN KOTA"

  replace NamaKecamatan = "MAKASAR"                if NamaKecamatan == "HALIM PERDANA KUSUMA"  & NamaKabupatenKota == "KOTA JAKARTA TIMUR"
  replace NamaDesaKel = "HALIM PERDANA KUSUMAH"    if NamaDesaKel == "KP MAKASAR"              & NamaKecamatan == "MAKASAR"

  replace NamaKecamatan = "JAKABARING"             if NamaKecamatan == "SEBERANG ULU"          & NamaKabupatenKota == "KOTA PALEMBANG"


* load PODES 2019 codes
  preserve
  import delimited "$agent_data/Supplementary data/master_wilayah_pendataan_potensi_desa_2019__desa__.csv", clear
  rename nama_kec   NamaKecamatan
  rename value_kab  idkab
  duplicates drop idkab NamaKecamatan, force
  keep nama_prov nama_kab idkab NamaKecamatan value_prov value_kec
  tempfile podes_codes_kec
  save `podes_codes_kec'
  restore

* merge at kecamatan level
  merge m:1 idkab NamaKecamatan using `podes_codes_kec'

  /*
    Result                           # of obs.
    -----------------------------------------
    not matched                         7,122
        from master                        37  (_merge==1)
        from using                      7,085  (_merge==2)

    matched                               723  (_merge==3)
    -----------------------------------------
  */

* keep only agents in matched kecamatans
  keep if _m == 3
  drop _m
  di _N

** Attempt merge at village level
  * fix spelling issues in village names
  replace NamaDesaKel = "KARANGETANG"                  if NamaDesaKel == "KARANGETAN"                  & NamaKecamatan == "DENGILO"
  replace NamaDesaKel = "KEMIRI"                       if NamaDesaKel == "KAMIRI"                      & NamaKecamatan == "PAGUAT"
  replace NamaDesaKel = "ILO HELUMA"                   if NamaDesaKel == "ILOHELUMA"                   & NamaKecamatan == "PATILANGGIO"
  replace NamaDesaKel = "BOTUBILOTAHU INDAH"           if NamaDesaKel == "BATUBILOTAHU"                & NamaKecamatan == "MARISA"
  replace NamaDesaKel = "WATANG BACUKIKI"              if NamaDesaKel == "WT. BACUKIKI"                & NamaKecamatan == "BACUKIKI"
  replace NamaDesaKel = "PACCERAKANG"                  if NamaDesaKel == "PACCERAKKANG"                & NamaKecamatan == "BIRING KANAYA"
  replace NamaDesaKel = "P A I"                        if NamaDesaKel == "P A I "                      & NamaKecamatan == "BIRING KANAYA"
  replace NamaDesaKel = "PATTINGALLOANG"               if NamaDesaKel == "PATTINGALOANG"               & NamaKecamatan == "UJUNG TANAH"
  replace NamaDesaKel = "MULYASRI"                     if NamaDesaKel == "MULYASARI"                   & NamaKecamatan == "TOMONI"
  replace NamaDesaKel = "MONCONGLOE LAPPARA"           if NamaDesaKel == "MONCONG LOE LAPPARA"         & NamaKecamatan == "MONCONGLOE"
  replace NamaDesaKel = "MONCONGLOE BULU"              if NamaDesaKel == "MONCONG LOE BULU"            & NamaKecamatan == "MONCONGLOE"
  replace NamaDesaKel = "MANNONGKOKI"                  if NamaDesaKel == "MANONGKOKI"                  & NamaKecamatan == "POLOMBANGKENG UTARA"
  replace NamaDesaKel = "PANYANGKALANG"                if NamaDesaKel == "PAYANGKALANG"                & NamaKecamatan == "MANGARA BOMBANG"
  replace NamaDesaKel = "KOTABANGON"                   if NamaDesaKel == "KOTOBANGUN"                  & NamaKecamatan == "KOTAMOBAGU TIMUR"
  replace NamaDesaKel = "POYOWA BESAR I"               if NamaDesaKel == "POYOWA BESAR SATU"           & NamaKecamatan == "KOTAMOBAGU SELATAN"
  replace NamaDesaKel = "MUARA JAWA PESISIR"           if NamaDesaKel == "MUARA JAWA"                  & NamaKecamatan == "MUARA JAWA"
  replace NamaDesaKel = "LOKTABAT SELATAN"             if NamaDesaKel == "LAKTABAT SELATAN"            & NamaKecamatan == "BANJAR BARU SELATAN"
  replace NamaDesaKel = "GUNTUNGMANGGIS"               if NamaDesaKel == "GUNTUNG MANGGIS"             & NamaKecamatan == "LANDASAN ULIN"
  replace NamaDesaKel = "SUNGAI PULAU"                 if NamaDesaKel == "SUNGAIPULAU"                 & NamaKecamatan == "PANGKALAN BANTENG"
  replace NamaDesaKel = "SUNGAI PAKIT"                 if NamaDesaKel == "SUNGAIPAKIT"                 & NamaKecamatan == "PANGKALAN BANTENG"
  replace NamaDesaKel = "SUNGAI KUNING"                if NamaDesaKel == "SUNGAIKUNING"                & NamaKecamatan == "PANGKALAN BANTENG"
  replace NamaDesaKel = "BUDAKELING"                   if NamaDesaKel == "BUDEKELING"                  & NamaKecamatan == "BEBANDEM"
  replace NamaDesaKel = "BHUANA GIRI"                  if NamaDesaKel == "BUANA GIRI"                  & NamaKecamatan == "BEBANDEM"
  replace NamaDesaKel = "BEBANDEM"                     if NamaDesaKel == "BEBANDEN"                    & NamaKecamatan == "BEBANDEM"
  replace NamaDesaKel = "TRI BUANA"                    if NamaDesaKel == "TRIBUANA"                    & NamaKecamatan == "ABANG"
  replace NamaDesaKel = "TIYINGTALI"                   if NamaDesaKel == "TIYING TALI"                 & NamaKecamatan == "ABANG"
  replace NamaDesaKel = "NAWAKERTI"                    if NamaDesaKel == "NAWA KERTI"                  & NamaKecamatan == "ABANG"
  replace NamaDesaKel = "LABA SARI"                    if NamaDesaKel == "LABASARI"                    & NamaKecamatan == "ABANG"
  replace NamaDesaKel = "TEGALLINGGAH"                 if NamaDesaKel == "TEGALINGGAH"                 & NamaKecamatan == "KARANGASEM"
  replace NamaDesaKel = "KARANGASEM"                   if NamaDesaKel == "KARANG ASEM"                 & NamaKecamatan == "KARANGASEM"
  replace NamaDesaKel = "PESEDAHAN"                    if NamaDesaKel == "PASEDAHAN"                   & NamaKecamatan == "MANGGIS"
  replace NamaDesaKel = "WISMA KERTA"                  if NamaDesaKel == "WISMAKERTA"                  & NamaKecamatan == "SIDEMEN"
  replace NamaDesaKel = "LOKA SARI"                    if NamaDesaKel == "LAKASARI"                    & NamaKecamatan == "SIDEMEN"
  replace NamaDesaKel = "KERTHA BUANA"                 if NamaDesaKel == "KERTA BUANA"                 & NamaKecamatan == "SIDEMEN"
  replace NamaDesaKel = "CANGAAN"                      if NamaDesaKel == `"CANGA"AN"'                  & NamaKecamatan == "UJUNGPANGKAH"
  replace NamaDesaKel = "GEDONGKEDOAN"                 if NamaDesaKel == `"KEDONG KEDO"AN"'            & NamaKecamatan == "DUKUN"
  replace NamaDesaKel = "BIMO MARTANI"                 if NamaDesaKel == "BIMOMARTANI"                 & NamaKecamatan == "NGEMPLAK"
  replace NamaDesaKel = "SUMBERREJO"                   if NamaDesaKel == "SUMBEREJO"                   & NamaKecamatan == "SEMIN"
  replace NamaDesaKel = "PUNDUNG SARI"                 if NamaDesaKel == "PUNDUNGSARI"                 & NamaKecamatan == "SEMIN"
  replace NamaDesaKel = "KARANG SARI"                  if NamaDesaKel == "KARANGSARI"                  & NamaKecamatan == "SEMIN"
  replace NamaDesaKel = "CANDI REJO"                   if NamaDesaKel == "CANDIREJO"                   & NamaKecamatan == "SEMIN"
  replace NamaDesaKel = "WATU SIGAR"                   if NamaDesaKel == "WATUSIGAR"                   & NamaKecamatan == "NGAWEN"
  replace NamaDesaKel = "JURANG JERO"                  if NamaDesaKel == "JURANGJERO"                  & NamaKecamatan == "NGAWEN"
  replace NamaDesaKel = "PILANG REJO"                  if NamaDesaKel == "PILANGREJO"                  & NamaKecamatan == "NGLIPAR"
  replace NamaDesaKel = "KEDUNG KERIS"                 if NamaDesaKel == "KEDUNGKERIS"                 & NamaKecamatan == "NGLIPAR"
  replace NamaDesaKel = "WATU GAJAH"                   if NamaDesaKel == "WATUGAJAH"                   & NamaKecamatan == "GEDANG SARI"
  replace NamaDesaKel = "HARGO MULYO"                  if NamaDesaKel == "HARGOMULYO"                  & NamaKecamatan == "GEDANG SARI"
  replace NamaDesaKel = "NGORO ORO"                    if NamaDesaKel == "NGORO-ORO"                   & NamaKecamatan == "PATUK"
  replace NamaDesaKel = "KARANG TENGAH"                if NamaDesaKel == "KARANGTENGAH"                & NamaKecamatan == "WONOSARI"
  replace NamaDesaKel = "UMBUL REJO"                   if NamaDesaKel == "UMBULREJO"                   & NamaKecamatan == "PONJONG"
  replace NamaDesaKel = "KARANG DUWET"                 if NamaDesaKel == "KARANGDUWET"                 & NamaKecamatan == "PALIYAN"
  replace NamaDesaKel = "KARANG ASEM"                  if NamaDesaKel == "KARANGASEM"                  & NamaKecamatan == "PALIYAN"
  replace NamaDesaKel = "PENDRIKAN KIDUL"              if NamaDesaKel == "PENDIRIKAN KIDUL"            & NamaKecamatan == "SEMARANG TENGAH"
  replace NamaDesaKel = "BANYUURIP"                    if NamaDesaKel == "BANYURIP"                    & NamaKecamatan == "SAMBUNG MACAN"
  replace NamaDesaKel = "BUTUH"                        if NamaDesaKel == "BUTUH LOR"                   & NamaKecamatan == "KALIKAJAR"
  replace NamaDesaKel = "KLEDUNG KRADENAN "            if NamaDesaKel == "KEDUNG KRADENAN"             & NamaKecamatan == "BANYU URIP"
  replace NamaDesaKel = "BEJI"                         if NamaDesaKel == "BEIJI"                       & NamaKecamatan == "BEJI"
  replace NamaDesaKel = "MEKAR JAYA"                   if NamaDesaKel == "MEKARJAYA"                   & NamaKecamatan == "SUKMAJAYA"
  replace NamaDesaKel = "BAKTI JAYA"                   if NamaDesaKel == "BHAKTI JAYA"                 & NamaKecamatan == "SUKMAJAYA"
  replace NamaDesaKel = "BAKTI JAYA"                   if NamaDesaKel == "BAKTIJAYA"                   & NamaKecamatan == "SUKMAJAYA"
  replace NamaDesaKel = "MEKARJAYA"                    if NamaDesaKel == "MEKAR MULYA"                 & NamaKecamatan == "RANCASARI"
  replace NamaDesaKel = "BARANANGSIANG"                if NamaDesaKel == "BARANANG SIANG"              & NamaKecamatan == "BOGOR TIMUR"
  replace NamaDesaKel = "CIKAMPEK UTARA"               if NamaDesaKel == "CILKAMPEK UTARA"             & NamaKecamatan == "KOTABARU"
  replace NamaDesaKel = "SAGALAHERANG"                 if NamaDesaKel == "SAGALAHERANG KIDUL"          & NamaKecamatan == "SAGALAHERANG"
  replace NamaDesaKel = "GLODOK"                       if NamaDesaKel == "GELODOK"                     & NamaKecamatan == "TAMAN SARI"
  replace NamaDesaKel = "TANJUNG DUREN SELATAN"        if NamaDesaKel == "TANJUNG DURENSELATAN"        & NamaKecamatan == "GROGOL PETAMBURAN"
  replace NamaDesaKel = "JATI PULO"                    if NamaDesaKel == "JATIPULO"                    & NamaKecamatan == "PALMERAH"
  replace NamaDesaKel = "CIPINANG BESAR SELATAN"       if NamaDesaKel == "CIPINGANG BESAR SELATAN"     & NamaKecamatan == "JATINEGARA"
  replace NamaDesaKel = "TANJUNGPINANG BARAT"          if NamaDesaKel == "TANJUNG PINANG BARAT"        & NamaKecamatan == "TANJUNGPINANG BARAT"
  replace NamaDesaKel = "SUKAJAWA"                     if NamaDesaKel == "SUKA JAWA"                   & NamaKecamatan == "TANJUNG KARANG BARAT"
  replace NamaDesaKel = "KEPUTERAAN"                   if NamaDesaKel == "KEPUTRAAN"                   & NamaKecamatan == "LUBUK LINGGAU BARAT II"
  replace NamaDesaKel = "SEKAR JAYA"                   if NamaDesaKel == "SEKARJAYA"                   & NamaKecamatan == "BATU RAJA TIMUR"
  replace NamaDesaKel = "KEMALA RAJA"                  if NamaDesaKel == "KEMALARAJA"                  & NamaKecamatan == "BATU RAJA TIMUR"
  replace NamaDesaKel = "AIR PAUH"                     if NamaDesaKel == "AIR PAOH"                    & NamaKecamatan == "BATU RAJA TIMUR"
  replace NamaDesaKel = "SIMPANG BABEKO"               if NamaDesaKel == "SIMPANG BEBEKO"              & NamaKecamatan == "BATHIN II BABEKO"
  replace NamaDesaKel = "TENGKERANG TENGAH"            if NamaDesaKel == "TENGKERENG TENGAH"           & NamaKecamatan == "MARPOYAN DAMAI"
  replace NamaDesaKel = "TENGKERANG LABUAI"            if NamaDesaKel == "TENGKERENG LABUAI"           & NamaKecamatan == "BUKIT RAYA"
  replace NamaDesaKel = "TENGKERANG SELATAN"           if NamaDesaKel == "TANGKERANG SELATAN"          & NamaKecamatan == "BUKIT RAYA"
  replace NamaDesaKel = "BALAI BATU SANDARAN"          if NamaDesaKel == "BALAI BATU SADARA"           & NamaKecamatan == "BARANGIN"
  replace NamaDesaKel = "TANJUNG SABA PITAMEH NAN XX"  if NamaDesaKel == "TANJUNG SABA PITAMENNAN XX"  & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "TANAH SIRAH PIAI NAN XX "     if NamaDesaKel == "TANAH SIRAH PIAI NANXX"      & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "PARAK LAWEH PULAU AIR NAN XX" if NamaDesaKel == "PARAK LAWEH PULAUAIA NAN XX" & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "PAGAMBIRAN AMPULU NAN XX"     if NamaDesaKel == "PAGAMBIRAN AMPALUNAN XX"     & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "LUBUK BEGALUNG NAN XX "       if NamaDesaKel == "LUBUK BEGALUNG NANXX"        & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "KAMPUNG BARU NAN XX"          if NamaDesaKel == "KAMPUNG BARU NANXX"          & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "CENGKEH NAN XX"               if NamaDesaKel == "CANGKEH NAN XX"              & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "BATUANG TABA NAN XX"          if NamaDesaKel == "BATUNG TABA NAN XX"          & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "PERKEBUNAN TELUK PANJI"       if NamaDesaKel == "PERK. TELUK PANJI"           & NamaKecamatan == "KAMPUNG RAKYAT"
  replace NamaDesaKel = "PERKEBUNAN PERLABIAN"         if NamaDesaKel == "PERK. PELABIAN"              & NamaKecamatan == "KAMPUNG RAKYAT"
  replace NamaDesaKel = "BINANGA DUA"                  if NamaDesaKel == "BINANGA II"                  & NamaKecamatan == "SILANGKITANG"
  replace NamaDesaKel = "PINANG DAME"                  if NamaDesaKel == "PINANG DAMAI"                & NamaKecamatan == "TORGAMBA"
  replace NamaDesaKel = "DOLAT RAYAT"                  if NamaDesaKel == "DOLAT RAKYAT"                & NamaKecamatan == "DOLAT RAYAT"
  replace NamaDesaKel = "TIGAPANAH"                    if NamaDesaKel == "TIGA PANAH"                  & NamaKecamatan == "TIGAPANAH"
  replace NamaDesaKel = "NARI GUNUNG DUA"              if NamaDesaKel == "NARI GUNUNG II"              & NamaKecamatan == "TIGANDERKET"
  replace NamaDesaKel = "PAYA BUJOK SEULEUMAK"         if NamaDesaKel == "PAYA BUJOK SEULEMAK"         & NamaKecamatan == "LANGSA BARO"
  replace NamaDesaKel = "IE MASEN ULEE KARENG"         if NamaDesaKel == "IEMASEN ULEE KARENG "        & NamaKecamatan == "ULEE KARENG"
  replace NamaDesaKel = "SEI KURUK III"                if NamaDesaKel == "SUNGAI KURUK III"            & NamaKecamatan == "SERUWAY"
  replace NamaDesaKel = "SEI KURUK II"                 if NamaDesaKel == "SUNGAI KURUK II"             & NamaKecamatan == "SERUWAY"
  replace NamaDesaKel = "ALUE JERJAK"                  if NamaDesaKel == "ALUE JEUREUJAK"              & NamaKecamatan == "BABAH ROT"
  replace NamaDesaKel = "IE LHOP"                      if NamaDesaKel == "IE LHOB"                     & NamaKecamatan == "TANGAN-TANGAN"
  replace NamaDesaKel = "KLEDUNG KRADENAN"             if NamaDesaKel == "KLEDUNG KRADENAN "           & NamaKecamatan == "BANYU URIP"
  replace NamaDesaKel = "PULO GADUNG"                  if NamaDesaKel == "PULOGADUNG"                  & NamaKecamatan == "PULO GADUNG"
  replace NamaDesaKel = "BATU SEMBILAN"                if NamaDesaKel == "BATU IX"                     & NamaKecamatan == "TANJUNGPINANG TIMUR"
  replace NamaDesaKel = "TANAH SIRAH PIAI NAN XX"      if NamaDesaKel == "TANAH SIRAH PIAI NAN XX "    & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "LUBUK BEGALUNG NAN XX"        if NamaDesaKel == "LUBUK BEGALUNG NAN XX "      & NamaKecamatan == "LUBUK BEGALUNG"
  replace NamaDesaKel = "KALI BARU"                    if NamaDesaKel == "KALIBARU"                    & NamaKecamatan == "CILINCING"
  replace NamaDesaKel = "PAPANGO"                      if NamaDesaKel == "PAPANGGO"                    & NamaKecamatan == "TANJUNG PRIOK"
  replace NamaDesaKel = "DALU 10 A"                    if NamaDesaKel == "DALU XA"                     & NamaKecamatan == "TANJUNG MORAWA"

  * probable matches
  replace NamaDesaKel = "JATINEGARA KAUM"              if NamaDesaKel == "JATINEGARA"                  & NamaKecamatan == "PULO GADUNG"
  replace NamaDesaKel = "WONGKADITI"                   if NamaDesaKel == "WANGKADITI TIMUR"            & NamaKecamatan == "KOTA UTARA"

* load podes data
  preserve
  import delimited "$agent_data/Supplementary data/master_wilayah_pendataan_potensi_desa_2019__desa__.csv", clear
  rename nama_desa NamaDesaKel
  duplicates tag value_kec NamaDesaKel, gen(tag)
  count if tag == 1
  list if tag == 1
  duplicates drop value_kec NamaDesaKel, force
  tempfile podes2019
  save `podes2019'
  restore

  * merge with villages
  merge m:1 value_kec NamaDesaKel using `podes2019'

  /*
    Result                           # of obs.
    -----------------------------------------
    not matched                        84,047
        from master                       300  (_merge==1)
        from using                     83,747  (_merge==2)

    matched                               423  (_merge==3)
    -----------------------------------------
  */

  * save matched agents
  keep if _m == 3
  drop _m
  count if tag == 1
  drop tag
  di _N
  ds
  rename value_desa kodewilayah
  keep KODEAGEN Nama* value* bank kodewilayah
  save "$agents_mar_18/matched_original_unmatched_agents_BAN2_BANK3.dta", replace

  cap log close
  // DONE
